package com.fingard.dsp.bank.directbank.icbc04;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.directbank.icbc04.interfacelist.ICBC04TransCode;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;

public class ICBC04RealTime extends ICBC04Base implements IRealTime {
    @Override
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9108);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9108.singleDetail.recAct, "");
            if (actItem != null ) {
                StringBuilder signBuilder = new StringBuilder();
                signBuilder.append(ICBC04TransCode.deal_9108_910801_81351);
                signBuilder.append("6002000293000010");
                signBuilder.append(String.format("%16s", p_req9108.singleDetail.askNumber));
                signBuilder.append(String.format("%12s", actItem.merchantId));
                signBuilder.append(String.format("%19s", p_req9108.singleDetail.payAct));
                signBuilder.append(String.format("%64s", getQryTransID()));
                signBuilder.append("001");
                signBuilder.append(String.format("%17s", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount)));
                signBuilder.append("0");
                signBuilder.append(String.format("%16s", ""));
                signBuilder.append(String.format("%23s", ""));

                String mac = mac(signBuilder.toString());
                WriteBankLogLn("mac:" + mac);

                StringBuilder tmpSendRequest = new StringBuilder();
                tmpSendRequest.append(String.format("%10s", "200"));
                tmpSendRequest.append(signBuilder);
                tmpSendRequest.append(leftFill(mac, 8, " "));

                String[] tmpStrRet = sendToBank(tmpSendRequest.toString());

                if (tmpStrRet[0].length() == 0) {
                    tmpRetDetail.respCode = "0";
                    if (StringHelper.isNullOrEmpty(tmpStrRet[1])) {
                        tmpRetDetail.respInfo = "银行响应为空,请联系银行咨询!";
                        tmpRetDetail.transState = PaymentState.FAIL_Payment;
                        tmpRetDetail.payInfoCode = FailCode.E9981;
                    } else {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = new String(tmpStrRet[1].getBytes(getCharset()), 5, 60, getCharset()).trim();
                        tmpRetDetail.payInfoCode = new String(tmpStrRet[1].getBytes(getCharset()), 0, 5, getCharset()).trim();
                        tmpRetDetail.transState = tmpRetDetail.payInfoCode;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            } else {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req9108.singleDetail.recAct + "]商户配置信息!";
                tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
                tmpRetDetail.transState = PaymentState.FAIL_Payment;
                tmpRetDetail.payInfoCode = FailCode.E9981;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9144);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req9144.recAct, "");
            if (actItem != null) {
                StringBuilder signBuilder = new StringBuilder();
                signBuilder.append(ICBC04TransCode.deal_9144_910801_71353);
                signBuilder.append("6002000293000010");
                signBuilder.append(String.format("%16s", getQryTransID()));//商户交易流水号
                signBuilder.append(String.format("%12s", actItem.merchantId));//商户号
                signBuilder.append(String.format("%19s", p_req9144.payAct));//银行卡号
                signBuilder.append(String.format("%16s", p_req9144.askNumber));
                signBuilder.append(Format.Date10Format.format(p_req9144.getBgnDateTime()));
                signBuilder.append("00");

                String mac = mac(signBuilder.toString());
                WriteBankLogLn("mac:" + mac);

                StringBuilder tmpSendRequest = new StringBuilder();
                tmpSendRequest.append(String.format("%10s", "104"));
                tmpSendRequest.append(signBuilder);
                tmpSendRequest.append(leftFill(mac, 8, " "));

                String[] tmpStrRet = sendToBank(tmpSendRequest.toString());

                if (tmpStrRet[0].length() == 0) {
                    tmpRetDetail.respCode = "0";
                    if (StringHelper.isNullOrEmpty(tmpStrRet[1])) {
                        tmpRetDetail.respInfo = "银行响应为空,请联系银行咨询!";
                        tmpRetDetail.transState = PaymentState.FAIL_Payment;
                        tmpRetDetail.payInfoCode = FailCode.E9981;
                    } else {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = new String(tmpStrRet[1].getBytes(getCharset()), 5, 60, getCharset()).trim();

                        String tmpRespCode = new String(tmpStrRet[1].getBytes(getCharset()), 0, 5, getCharset()).trim();
                        tmpRetDetail.payInfoCode = tmpRespCode;
                        String status = new String(tmpStrRet[1].getBytes(getCharset()), 65, 2).trim();
                        if (StringHelper.isNullOrEmpty(status)) {
                            tmpRetDetail.transState = tmpRespCode;
                        } else {
                            tmpRetDetail.transState = status;
                        }
                        if ("0901".equalsIgnoreCase(tmpRespCode)) {
                            if (canBeNotExist(p_req9144, tmpRetDetail, 45)) {
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.transState = tmpRespCode;
                                tmpRetDetail.payInfoCode = tmpRespCode;
                                retAts.writeRetDetail(tmpRetDetail);
                                return retAts;
                            }
                        }
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号:[" + p_req9144.recAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}